package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dto.OrderDto;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.OrderSubmitParam;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 预约控制器
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
@Api(tags = "手机端订单接口")
public class MobileOrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 任务10-提交预约
     * 1.OrderSubmitParam
     * 2.验证码
     * 3.创建会员-判断会员是否已存在-如果不是会员自动注册为会员，老会员不能重复预约
     * 4.提交体检信息 order
     * 5.?检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约orderstting
     * 6.?预约人数是否满了？查询数据库orderstting通过日期
     * 7.?检查用户是否重复预约（同一个用户在同一天预约了同一个套餐）order?
     * 8.?预约成功，更新当日的已预约人数orderstting
     * @return
     */
    @PostMapping("/submit")         //1.OrderSubmitParam
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam) {
        //2.验证码
        String redisKey = RedisConst.VALIDATE_CODE_PREFIX + "_ORDER_" + orderSubmitParam.getTelephone();
        String code = redisTemplate.opsForValue().get(redisKey).toString();
        //前端传来的数据
        String name = orderSubmitParam.getName();
        String sex = orderSubmitParam.getSex();
        String telephone = orderSubmitParam.getTelephone();
        String idCard = orderSubmitParam.getIdCard();
        Date orderDate = orderSubmitParam.getOrderDate();
        Integer setMealId = orderSubmitParam.getSetMealId();
        //3.创建会员
        Member member = new Member();
        member.setName(name);
        member.setSex(sex);
        member.setPhoneNumber(telephone);
        member.setIdCard(idCard);
        //判断会员是否已存在-如果不是会员自动注册为会员，老会员不能重复预约
        member = memberService.createOrUpdate(member);
        //4.提交体检信息  order
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setMealId);
        //5.?检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约orderstting
        //6.?预约人数是否满了？查询数据库orderstting通过日期
        //7.?检查用户是否重复预约（同一个用户在同一天预约了同一个套餐）order?
        //8.?预约成功，更新当日的已预约人数orderstting
        order = orderService.addOrder(order);

        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }
        //任务11 体检预约-订单数据展示
        @GetMapping("/findById")
        public Result findById(Integer id){
            //查询订单数据
            OrderDto orderDto = orderService.getOrderData(id);
            return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, orderDto);
        }
}
